/****************************************************************************
**
** UOString.h
**
** Copyright (C) August 2014 Hotride
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
*****************************************************************************
*/
//---------------------------------------------------------------------------
#ifndef UOStringH
#define UOStringH
//---------------------------------------------------------------------------
class String
{
private:
	char *m_Data;
	wchar_t *m_UnicodeData;

	int m_Length;
	int m_Position;

	bool m_UnicodePriority;

public:

	template <typename T1, typename T2>
	static inline vector<T1> _split_internal(T1 str, T2 dlm, int &maxsplit)
	{
		vector<T1> result;
		size_t string_pos = 0; // current string position	
		size_t delim_pos = 0; // delimiter pos
		size_t token_pos = 0; // token or word position

		while ((token_pos = str.find_first_not_of(dlm, string_pos)) != string::npos) {

			if (((delim_pos = str.find_first_of(dlm, token_pos)) != string::npos) && maxsplit) {
				result.push_back(str.substr(token_pos, delim_pos - token_pos));
				string_pos = delim_pos + 1;

				if (maxsplit > 0)
					maxsplit -= 1;
			}
			else {
				break;
			}
		}

		if (token_pos != string::npos) {
			result.push_back(str.substr(token_pos, string::npos));
		}
		return result;
	}

	// spilt a string with delimiter delim specified
	static vector<string> split(string str, char* delim = " ", int maxsplit = -1);

    // spilt an utf-16 string with delimiter delim specified
	static vector<wstring> split(wstring str, wchar_t* delim = L" ", int maxsplit = -1);

	String();
	String(const char *val);
	String(const char val);
	String(const wchar_t *val);
	String(const wchar_t val);
	String(const int val);
	String(const string &s);
	String(const wstring &s);
	String(const String &s);

	~String();
	
	String *m_Next;
	String *m_Prev;
	
	bool operator==(const String &s);
	bool operator==(const char *s);
	bool operator==(const wchar_t *s);
	
	bool operator!=(const String &s);
	bool operator!=(const char *s);
	bool operator!=(const wchar_t *s);

	String operator+(const String& s);
	String operator+=(const String& s);
	String operator=(const String& s);
	String operator=(const char *s);
	String operator=(const char s);
	String operator=(const wchar_t *s);
	String operator=(const wchar_t s);
	String operator=(const int s);

	int Length() const {return m_Length;}
	
	const char *c_str() const;
	const wchar_t *w_str() const;
	
	String TrimRight();
	String TrimLeft();
	String Trim();
	
	String UpperCase();
	String LowerCase();
	
	String Insert(const String &s, int pos);
	String Delete(int index, int count = 1);
	
	int Pos(const String &s);
	int NextPos(const String &s);
	int LastPos(const String &s);
	int EndPos(const String &s);
	
	void SetLength(int new_len);
	
	wstring ToUnicode();
	string ToASCII();

	bool UnicodePriority() const {return m_UnicodePriority;}
};
//---------------------------------------------------------------------------
class StringList
{
private:
	String *m_Head;
	
	int m_Size;

public:
	StringList();
	~StringList();
	
	String *operator[](const int pos);

	int Size() const {return m_Size;}

	void Clear();
	
	void Add(const String &s);
	void Insert(const String &s, int pos);
	void Delete(int pos);

};
//---------------------------------------------------------------------------
#endif